<template>
  <div class="bdsharebuttonbox">
    <a
      v-for="item in defaultConfig.shareList"
      :key="item.id"
      :class="'bds_' + item.name"
      :data-cmd="item.name"
    ></a>
  </div>
</template>

<script>
export default {
  data() {
    return {
      defaultConfig: {
        shareList: ["more", "qzone", "tsina", "tqq", "renren", "weixin"],
        common: {},
        share: [{ bdSize: 24 }],
        slide: false,
        image: false,
        selectShare: false
      },
      readyPromise: null,
      readyPromiseRS: null,
      readyPromiseRJ: null
    };
  },
  props: {
    vshareConfig: {
      type: Object
    }
  },
  created() {
    this.readyPromise = new Promise((resolve, reject) => {
      this.readyPromiseRS = resolve;
      this.readyPromiseRJ = reject;
    });
  },
  mounted() {
    if (this.vshareConfig) {
      this.vshareConfig.shareList
        ? (this.defaultConfig.shareList = this.vshareConfig.shareList)
        : "";
      if (this.vshareConfig.common) {
        this.defaultConfig.common = this.vshareConfig.common;
      }
      if (this.vshareConfig.share) {
        this.defaultConfig.share = this.vshareConfig.share;
      }
      if (this.vshareConfig.slide) {
        this.defaultConfig.slide = this.vshareConfig.slide;
      }
      if (this.vshareConfig.image) {
        this.defaultConfig.image = this.vshareConfig.image;
      }
      if (this.vshareConfig.selectShare) {
        this.defaultConfig.selectShare = this.vshareConfig.selectShare;
      }
    }
    this.readyPromiseRS();
  },
  watch: {
    vshareConfig: {
      handler(val) {
        if (val.common.bdUrl) {
          this.readyPromise.then(() => {
            window._bd_share_config = this.defaultConfig;
            document
              .getElementsByTagName("body")[0]
              .appendChild(document.createElement("script")).src =
              "/static/api/js/share.js";
          });
        }
      },
      deep: true,
      immediate: true
    }
  }
};
</script>
